home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs1.zip / UUCICO / ulibip.c < prev    next >
C/C++ Source or Header  |  1993-10-02  |  27KB  |  814 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       u l i b i p . c                                              */
  3. /*                                                                    */
  4. /*       TCP/IP port communications driver for Windows sockets        */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Copyright (c) David M. Watt 1993, All Rights Reserved           */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  13. /*    Wonderworks.                                                    */
  14. /*                                                                    */
  15. /*    All rights reserved except those explicitly granted by the      */
  16. /*    UUPC/extended license agreement.                                */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                          RCS Information                           */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. /*
  24.  *    $Id: ulibip.c 1.6 1993/10/02 23:12:35 dmwatt Exp $
  25.  *
  26.  *    $Log: ulibip.c $
  27.  * Revision 1.6  1993/10/02  23:12:35  dmwatt
  28.  * Winsock error message support
  29.  *
  30.  * Revision 1.5  1993/09/26  03:32:27  dmwatt
  31.  * Use Standard Windows NT error message module
  32.  *
  33.  * Revision 1.4  1993/09/25  03:07:56  ahd
  34.  * Addition error traps by Dave Watt
  35.  *
  36.  * Revision 1.3  1993/09/23  03:26:51  ahd
  37.  * Correct setting of carrier detect
  38.  *
  39.  * Revision 1.2  1993/09/21  01:42:13  ahd
  40.  * Use standard MAXPACK limit for save buffer size
  41.  *
  42.  * Revision 1.1  1993/09/20  04:48:25  ahd
  43.  * Initial revision
  44.  *
  45.  */
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*                        System include files                        */
  49. /*--------------------------------------------------------------------*/
  50.  
  51. #include <windows.h>
  52. #include "winsock.h"
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <time.h>
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                    UUPC/extended include files                     */
  61. /*--------------------------------------------------------------------*/
  62.  
  63. #include "lib.h"
  64. #include "hlib.h"
  65. #include "ulib.h"
  66. #include "comm.h"          // Modem status bits
  67. #include "ssleep.h"
  68. #include "catcher.h"
  69.  
  70. #include "commlib.h"       // Trace functions, etc.
  71. #include "pwserr.h"        // Windows sockets error messages
  72.  
  73. #ifdef _Windows
  74. #include "pwinsock.h"      // definitions for 16 bit Winsock functions
  75. #endif
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*                        Internal prototypes                         */
  79. /*--------------------------------------------------------------------*/
  80.  
  81. void AtWinsockExit(void);
  82. boolean IsFatalSocketError(int err);
  83. void tcloseline(void);
  84.  
  85. /*--------------------------------------------------------------------*/
  86. /*                          Global variables                          */
  87. /*--------------------------------------------------------------------*/
  88.  
  89. static boolean carrierDetect = FALSE;
  90.  
  91. currentfile();
  92. static boolean hangupNeeded = TRUE;
  93. extern boolean winsockActive;                   // Initialized in catcher.c
  94. static SOCKET pollingSock = INVALID_SOCKET;     // The current polling socket
  95. static SOCKET connectedSock = INVALID_SOCKET;   // The currently connected socket
  96. static boolean connectionDied = FALSE;          // The current connection failed
  97.  
  98. /*--------------------------------------------------------------------*/
  99. /*                           Local defines                            */
  100. /*--------------------------------------------------------------------*/
  101.  
  102. #ifndef MAKEWORD
  103. #define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8))
  104. #endif
  105.  
  106. /*--------------------------------------------------------------------*/
  107. /*    I n i t W i n s o c k                                           */
  108. /*                                                                    */
  109. /*    Start the Windows sockets DLL                                   */
  110. /*--------------------------------------------------------------------*/
  111.  
  112. boolean InitWinsock(void)
  113. {
  114.    WSADATA WSAData;
  115.    int status;
  116.    static boolean firstPass = TRUE;
  117.  
  118.    if ( winsockActive )
  119.       return TRUE;
  120.  
  121. /*--------------------------------------------------------------------*/
  122. /*       The atexit() must precede the WSAStartup() so the            */
  123. /*       FreeLibrary() call gets done                                 */
  124. /*--------------------------------------------------------------------*/
  125.  
  126.    if ( firstPass )
  127.    {
  128.       firstPass = FALSE;
  129.       atexit(AtWinsockExit);
  130.    }
  131.  
  132. #ifdef _Windows
  133.    if (!pWinSockInit())
  134.       return FALSE;
  135. #endif
  136.  
  137. // status = WSAStartup(MAKEWORD(1,1), &WSAData);
  138.    status = WSAStartup(0x0101, &WSAData);
  139.  
  140.    if (status != 0)
  141.    {
  142.       printf("WSAStartup Error: %d", status);
  143.       return FALSE;
  144.    }
  145.  
  146.    winsockActive = TRUE;
  147.    return TRUE;
  148.  
  149. } /* InitWinsock */
  150.  
  151. /*--------------------------------------------------------------------*/
  152. /*       A t W i n s o c k E x i t                                    */
  153. /*                                                                    */
  154. /*       Clean up Windows DLL at shutdown                             */
  155. /*--------------------------------------------------------------------*/
  156.  
  157.  
  158. void AtWinsockExit(void)
  159. {
  160.    WSACleanup();
  161.  
  162. #ifdef _Windows
  163.    pWinSockExit();
  164. #endif
  165.  
  166.    winsockActive = FALSE;
  167.  
  168. }  /* AtWinsockExit */
  169.  
  170. /*--------------------------------------------------------------------*/
  171. /*    t o p e n a c t i v e                                           */
  172. /*                                                                    */
  173. /*    Open an active socket connection for I/O                        */
  174. /*--------------------------------------------------------------------*/
  175.  
  176. #ifdef __TURBOC__
  177. #pragma argsused
  178. #endif
  179.  
  180. int tactiveopenline(char *name, BPS bps, const boolean direct)
  181. {
  182.    SOCKADDR_IN sin;
  183.    LPHOSTENT phe;
  184.    LPSERVENT pse;
  185.  
  186.    if (!InitWinsock())           // Initialize library?
  187.       return TRUE;               // No --> Report error
  188.  
  189.    if (portActive)              /* Was the port already active?      */
  190.       closeline();               /* Yes --> Shutdown it before open  */
  191.  
  192.    printmsg(15, "tactiveopenline: %s", name);
  193.  
  194.    norecovery = FALSE;     // Flag we need a graceful shutdown after
  195.                            // Ctrl-BREAK
  196.  
  197.    carrierDetect = FALSE;  /* No modem connected yet                */
  198.  
  199.    connectionDied = FALSE; /* The connection hasn't failed yet */
  200.  
  201. /*--------------------------------------------------------------------*/
  202. /*                        Get remote host name                        */
  203. /*--------------------------------------------------------------------*/
  204.  
  205.    sin.sin_family = AF_INET;
  206.    phe = gethostbyname(name);
  207.  
  208.    if (phe)
  209. #ifdef _Windows
  210.       _fmemcpy((char FAR *) &(sin.sin_addr),
  211.                (char FAR *) phe->h_addr,
  212.                phe->h_length);
  213. #else
  214.       memcpy((char FAR *) &(sin.sin_addr),
  215.              (char FAR *) phe->h_addr,
  216.              phe->h_length);
  217. #endif
  218.    else {
  219.       sin.sin_addr.s_addr = inet_addr(name);
  220.  
  221.       if ( sin.sin_addr.s_addr == INADDR_NONE )
  222.       {
  223.          int wsErr = WSAGetLastError();
  224.  
  225.          printmsg(0, "tactiveopenline: "
  226.             "Is '%s' listed in the hosts file or a valid IP address?",
  227.             name);
  228.          printWSerror("gethostbyname", wsErr);
  229.          return TRUE;
  230.       }
  231.  
  232.    } /* else */
  233.  
  234. /*--------------------------------------------------------------------*/
  235. /*                     Get the TCP/IP port number                     */
  236. /*--------------------------------------------------------------------*/
  237.  
  238.    pse = getservbyname("uucp", "tcp");
  239.    if (pse == NULL)
  240.    {
  241.       int wsErr = WSAGetLastError();
  242.  
  243.       sin.sin_port = 540;
  244.       printWSerror("getservbyname", wsErr);
  245.       printmsg(0, "tactiveopenline: using port %d", (int)sin.sin_port);
  246.    }
  247.    else
  248.       sin.sin_port = pse->s_port;
  249.  
  250.    connectedSock = socket( AF_INET, SOCK_STREAM, 0);
  251.    if (connectedSock == INVALID_SOCKET)
  252.    {
  253.       printmsg(0, "tactiveopenline: socket() failed");
  254.       return TRUE;
  255.    }
  256.  
  257.    if (connect( connectedSock, (PSOCKADDR) &sin, sizeof(sin)) < 0)
  258.    {
  259.       int wsErr = WSAGetLastError();
  260.  
  261.       printmsg(0, "tactiveopenline: connect() failed");
  262.       printWSerror("connect", wsErr);
  263.       closesocket( connectedSock );
  264.       connectedSock = INVALID_SOCKET;
  265.  
  266.       return TRUE;
  267.    }
  268.  
  269.    traceStart( name );
  270.  
  271.    portActive = TRUE;     /* record status for error handler */
  272.    carrierDetect = TRUE;   // Carrier detect = connection
  273.  
  274.    return FALSE;                       // Return success to caller
  275.  
  276. } /* tactiveopenline */
  277.  
  278. /*--------------------------------------------------------------------*/
  279. /*    t o p e n p a s s i v e                                         */
  280. /*                                                                    */
  281. /*    Listen on a socket for an incoming uucp connection              */
  282. /*--------------------------------------------------------------------*/
  283.  
  284. #ifdef __TURBOC__
  285. #pragma argsused
  286. #endif
  287.  
  288. int tpassiveopenline(char *name, BPS bps, const boolean direct)
  289. {
  290.    SOCKADDR_IN sin;
  291.    LPSERVENT pse;
  292.  
  293.    if (!InitWinsock())           // Initialize library?
  294.       return TRUE;               // No --> Report error
  295.  
  296.    if (portActive)              /* Was the port already active?      */
  297.       closeline();               /* Yes --> Shutdown it before open  */
  298.  
  299.    printmsg(15, "tpassiveopenline: opening passive connection");
  300.  
  301.    norecovery = FALSE;     // Flag we need a graceful shutdown after
  302.                            // Ctrl-BREAK
  303.  
  304.    carrierDetect = FALSE;  /* No network connection yet             */
  305.  
  306.    connectionDied = FALSE; /* The connection hasn't failed yet */
  307.  
  308. /*--------------------------------------------------------------------*/
  309. /*                    Fill in host and family info                    */
  310. /*--------------------------------------------------------------------*/
  311.  
  312.    sin.sin_family = AF_INET;
  313.  
  314. /*--------------------------------------------------------------------*/
  315. /*                Fill in service information for tcp                 */
  316. /*--------------------------------------------------------------------*/
  317.  
  318.    printmsg(15, "tpassiveopenline: doing getservbyname");
  319.    pse = getservbyname("uucp", "tcp");
  320.  
  321.    if (pse == NULL)
  322.    {
  323.       int wsErr = WSAGetLastError();
  324.  
  325.       sin.sin_port = 540;
  326.       printWSerror("getservbyname", wsErr);
  327.       printmsg(0, "tpassiveopenline: using port %d",
  328.                   (int) sin.sin_port);
  329.    }
  330.    else
  331.       sin.sin_port = pse->s_port;
  332.  
  333.    sin.sin_addr.s_addr = 0;
  334.  
  335.    printmsg(5, "tpassiveopenline: waiting on port %d",
  336.                (int)ntohs(sin.sin_port));
  337.  
  338. /*--------------------------------------------------------------------*/
  339. /*                     Create and bind TCP socket                     */
  340. /*--------------------------------------------------------------------*/
  341.  
  342.    printmsg(15, "tpassiveopen: doing socket()");
  343.    pollingSock = socket( AF_INET, SOCK_STREAM, 0);
  344.  
  345.    if (pollingSock == INVALID_SOCKET)
  346.    {
  347.       int wsErr = WSAGetLastError();
  348.  
  349.       printmsg(0, "tpassiveopen: socket() failed");
  350.       printWSerror("socket", wsErr);
  351.       return TRUE;
  352.    }
  353.  
  354.    printmsg(15, "tpassiveopen: doing bind()");
  355.  
  356.    if (bind(pollingSock,
  357.            (struct sockaddr FAR *) &sin,
  358.            sizeof(sin)) == SOCKET_ERROR)
  359.    {
  360.       int wsErr = WSAGetLastError();
  361.  
  362.       printmsg(0, "tpassiveopen: bind(pollingSock) failed");
  363.       printWSerror("bind", wsErr);
  364.       return TRUE;                      // report failure
  365.    }
  366.  
  367.    printmsg(15, "tpassiveopen: doing listen()");
  368.    if (listen(pollingSock, 2) == SOCKET_ERROR)
  369.    {
  370.       int wsErr = WSAGetLastError();
  371.  
  372.       printmsg(0, "tpassiveopen: listen(pollingSock) failed");
  373.       printWSerror("listen", wsErr);
  374.       return TRUE;
  375.    }
  376.  
  377.    traceStart( name );
  378.  
  379.    portActive = TRUE;     /* record status for error handler */
  380.  
  381.    return FALSE;          // Return success to caller
  382.  
  383. } /* tpassiveopen */
  384.  
  385. /*--------------------------------------------------------------------*/
  386. /*    t s r e a d                                                     */
  387. /*                                                                    */
  388. /*    Read from the network socket                                    */
  389. /*                                                                    */
  390. /*    Non-blocking read essential to "g" protocol.  See               */
  391. /*    "dcpgpkt.c" for description.  This all changes in a             */
  392. /*    multi-tasking system.  Requests for I/O should get queued       */
  393. /*    and an event flag given.  Then the requesting process (e.g.     */
  394. /*    gmachine()) waits for the event flag to fire processing         */
  395. /*    either a read or a write.  Could be implemented on VAX/VMS      */
  396. /*    or DG but not MS-DOS.                                           */
  397. /*--------------------------------------------------------------------*/
  398.  
  399. unsigned int tsread(char *output, unsigned int wanted, unsigned int timeout)
  400. {
  401.    fd_set readfds;
  402.    struct timeval tm;
  403.    int nReady;
  404.    static char save[MAXPACK];
  405.    static unsigned short bufsize = 0;
  406.    time_t stop_time ;
  407.    time_t now ;
  408.  
  409. /*--------------------------------------------------------------------*/
  410. /*           Determine if our internal buffer has the data            */
  411. /*--------------------------------------------------------------------*/
  412.  
  413.    if (bufsize >= wanted)
  414.    {
  415.       memmove( output, save, wanted );
  416.       bufsize -= wanted;
  417.       if ( bufsize )          /* Any data left over?                 */
  418.          memmove( save, &save[wanted], bufsize );  /* Yes --> Save it*/
  419.       return wanted + bufsize;
  420.    } /* if */
  421.  
  422.    if (connectionDied || connectedSock == INVALID_SOCKET)
  423.    {                             // Haven't accepted a connection yet?
  424.       return 0;
  425.    }
  426.  
  427. /*--------------------------------------------------------------------*/
  428. /*                 Determine when to stop processing                  */
  429. /*--------------------------------------------------------------------*/
  430.  
  431.    if ( timeout == 0 )
  432.    {
  433.       stop_time = 0;
  434.       now = 1;                /* Any number greater than stop time   */
  435.    }
  436.    else {
  437.       time( & now );
  438.       stop_time = now + timeout;
  439.    }
  440.  
  441.    do {
  442.       int received;
  443.       int needed = wanted - bufsize;
  444.  
  445. /*--------------------------------------------------------------------*/
  446. /*          Initialize fd_set structure for select() call             */
  447. /*--------------------------------------------------------------------*/
  448.  
  449.       FD_ZERO(&readfds);
  450.       FD_SET(connectedSock, &readfds);
  451.  
  452. /*--------------------------------------------------------------------*/
  453. /*                     Handle an aborted program                      */
  454. /*--------------------------------------------------------------------*/
  455.  
  456.       if ( terminate_processing )
  457.       {
  458.          static boolean recurse = FALSE;
  459.          if ( ! recurse )
  460.          {
  461.             printmsg(2,"sread: User aborted processing");
  462.             recurse = TRUE;
  463.          }
  464.          return 0;
  465.       }
  466.  
  467. /*--------------------------------------------------------------------*/
  468. /*       Special case for network sockets: block for at least 5      */
  469. /*       msec if we have to read at least one character (this         */
  470. /*       needs to be tuned)                                           */
  471. /*--------------------------------------------------------------------*/
  472.  
  473.       if (stop_time > now )
  474.       {
  475.          tm.tv_sec = stop_time - now;
  476.          tm.tv_usec = 0;
  477.       }
  478.       else {
  479.  
  480.          tm.tv_usec = 5000;
  481.          tm.tv_sec = 0;
  482.       }
  483.  
  484. /*--------------------------------------------------------------------*/
  485. /*                 Read the data from the socket                      */
  486. /*--------------------------------------------------------------------*/
  487.  
  488.       nReady = select(1, &readfds, NULL, NULL, &tm);
  489.       if (nReady == SOCKET_ERROR)
  490.       {
  491.          int err = WSAGetLastError();
  492.          printmsg(0, "tsread: error in select()");
  493.          printWSerror("select", err);
  494.  
  495.          if (IsFatalSocketError(err))
  496.          {
  497.             shutdown(connectedSock, 2);  // Fail both reads and writes
  498.             connectionDied = TRUE;
  499.          }
  500.          bufsize = 0;
  501.          return 0;
  502.       }
  503.       else if (nReady == 0)
  504.       {
  505.          printmsg(5, "tsread: timeout after %d seconds",timeout);
  506.          bufsize = 0;
  507.          return 0;
  508.       }
  509.       else {
  510.          received = recv(connectedSock, &save[bufsize], needed, 0);
  511.          if (received == SOCKET_ERROR)
  512.          {
  513.             int wsErr = WSAGetLastError();
  514.  
  515.             printmsg(0, "tsread: recv() failed");
  516.             printWSerror("recv", wsErr);
  517.             bufsize = 0;
  518.             return 0;
  519.          }
  520.       }  /* else */
  521.  
  522. #ifdef UDEBUG
  523.       printmsg(15,"sread: Want %d characters, received %d, total %d in buffer",
  524.             (int) wanted, (int) received, (int) bufsize + received);
  525. #endif
  526.  
  527. /*--------------------------------------------------------------------*/
  528. /*                    Log the newly received data                     */
  529. /*--------------------------------------------------------------------*/
  530.  
  531.       traceData( &save[bufsize], received, FALSE );
  532.  
  533. /*--------------------------------------------------------------------*/
  534. /*            If we got the data, return it to the caller             */
  535. /*--------------------------------------------------------------------*/
  536.  
  537.       bufsize += received;
  538.       if ( bufsize == wanted )
  539.       {
  540.          memmove( output, save, bufsize);
  541.          bufsize = 0;
  542.  
  543.          if (debuglevel > 14)
  544.             fwrite(output,1,bufsize,stdout);
  545.  
  546.          return wanted;
  547.       } /* if */
  548.  
  549. /*--------------------------------------------------------------------*/
  550. /*                 Update the clock for the next pass                 */
  551. /*--------------------------------------------------------------------*/
  552.  
  553.       if (stop_time > 0)
  554.          time( &now );
  555.  
  556.    } while (stop_time > now);
  557.  
  558. /*--------------------------------------------------------------------*/
  559. /*         We don't have enough data; report what we do have          */
  560. /*--------------------------------------------------------------------*/
  561.  
  562.    return bufsize;
  563.  
  564. } /* tsread */
  565.  
  566. /*--------------------------------------------------------------------*/
  567. /*    t s w r i t e                                                   */
  568. /*                                                                    */
  569. /*    Write to the open socket                                        */
  570. /*   Note:  this is non-blocking, so we've got to use select() to     */
  571. /*    gradually write out the entire buffer                           */
  572. /*--------------------------------------------------------------------*/
  573.  
  574. int tswrite(char *data, unsigned int len)
  575. {
  576.    int status;
  577.  
  578. /* Has connection died? */
  579.    if (connectionDied || connectedSock == INVALID_SOCKET)
  580.       return 0;
  581.  
  582.    status = send(connectedSock, data, len, 0);
  583.  
  584.    if (status == SOCKET_ERROR)
  585.    {
  586.       int err;
  587.  
  588.       err = WSAGetLastError();
  589.       printmsg(0, "tswrite: Error sending data to socket");
  590.       printWSerror("send", err);
  591.  
  592.       if (IsFatalSocketError(err))
  593.       {
  594.          shutdown(connectedSock, 2);  // Fail both reads and writes
  595.          connectionDied = TRUE;
  596.       }
  597.       return 0;
  598.    }
  599.  
  600.    if (status < len)
  601.    {
  602.       printmsg(0,"tswrite: Write to network failed.");
  603.       return status;
  604.    }
  605.  
  606. /*--------------------------------------------------------------------*/
  607. /*                        Log the data written                        */
  608. /*--------------------------------------------------------------------*/
  609.  
  610.    traceData( data, len, TRUE );
  611.  
  612. /*--------------------------------------------------------------------*/
  613. /*              Return byte count transmitted to caller               */
  614. /*--------------------------------------------------------------------*/
  615.  
  616.    return len;
  617.  
  618. } /* tswrite */
  619.  
  620. /*--------------------------------------------------------------------*/
  621. /*    t s s e n d b r k                                               */
  622. /*                                                                    */
  623. /*    Send a break signal over the network                            */
  624. /*--------------------------------------------------------------------*/
  625.  
  626. #ifdef __TURBOC__
  627. #pragma argsused
  628. #endif
  629.  
  630. void tssendbrk(unsigned int duration)
  631. {
  632.    printmsg(12, "tsendbrk: ignored break of duration %d", duration);
  633.    return;
  634.  
  635. } /* tssendbrk */
  636.  
  637. /*--------------------------------------------------------------------*/
  638. /*    t c l o s e l i n e                                             */
  639. /*                                                                    */
  640. /*    Close the serial port down                                      */
  641. /*--------------------------------------------------------------------*/
  642.  
  643. void tcloseline(void)
  644. {
  645.    if (!portActive)
  646.       panic();
  647.  
  648.    portActive = FALSE;     /* flag port closed for error handler  */
  649.  
  650.    if (connectedSock != INVALID_SOCKET)
  651.    {
  652.       closesocket(connectedSock);
  653.       connectedSock = INVALID_SOCKET;
  654.    }
  655.  
  656.    if (pollingSock != INVALID_SOCKET)
  657.    {
  658.       closesocket(pollingSock);
  659.       pollingSock = INVALID_SOCKET;
  660.    }
  661.  
  662.    carrierDetect = FALSE;  /* No network connection yet               */
  663.    traceStop();
  664.  
  665. } /* tcloseline */
  666.  
  667. /*--------------------------------------------------------------------*/
  668. /*    t h a n g u p                                                   */
  669. /*                                                                    */
  670. /*    Break the connection with the remote system.                    */
  671. /*--------------------------------------------------------------------*/
  672.  
  673. void thangup( void )
  674. {
  675.    if (!hangupNeeded)
  676.       return;
  677.    hangupNeeded = FALSE;
  678.  
  679.    if (connectedSock != INVALID_SOCKET)
  680.    {
  681.       closesocket(connectedSock);
  682.       connectedSock = INVALID_SOCKET;
  683.    }
  684.  
  685.    if (pollingSock != INVALID_SOCKET)
  686.    {
  687.       closesocket(pollingSock);
  688.       pollingSock = INVALID_SOCKET;
  689.    }
  690.  
  691.    carrierDetect = FALSE;  /* No network connection yet               */
  692.  
  693. } /* thangup */
  694.  
  695. /*--------------------------------------------------------------------*/
  696. /*    t S I O S p e e d                                               */
  697. /*                                                                    */
  698. /*    Re-specify the speed of an opened serial port                   */
  699. /*--------------------------------------------------------------------*/
  700.  
  701. #ifdef __TURBOC__
  702. #pragma argsused
  703. #endif
  704.  
  705. void tSIOSpeed(BPS bps)
  706. {
  707.    return;  /* Irrelevant on network */
  708. } /* iSIOSpeed */
  709.  
  710. /*--------------------------------------------------------------------*/
  711. /*    t f l o w c o n t r o l                                         */
  712. /*                                                                    */
  713. /*    Enable/Disable in band (XON/XOFF) flow control                  */
  714. /*--------------------------------------------------------------------*/
  715.  
  716. #ifdef __TURBOC__
  717. #pragma argsused
  718. #endif
  719.  
  720. void tflowcontrol( boolean flow )
  721. {
  722.    return;                  /* Irrelevant on network (we hope)       */
  723. } /* tflowcontrol */
  724.  
  725. /*--------------------------------------------------------------------*/
  726. /*    t G e t S p e e d                                               */
  727. /*                                                                    */
  728. /*    Report current speed of communications connection               */
  729. /*--------------------------------------------------------------------*/
  730.  
  731. BPS tGetSpeed( void )
  732. {
  733.    return 57600;           // Arbitary large number to avoid possible
  734.                            // divide by zero error in caller
  735. } /* GetSpeed */
  736.  
  737. /*--------------------------------------------------------------------*/
  738. /*    t C D                                                           */
  739. /*                                                                    */
  740. /*    Report if we have carrier detect and lost it                    */
  741. /*--------------------------------------------------------------------*/
  742.  
  743. boolean tCD( void )
  744. {
  745.    boolean online = carrierDetect;
  746.  
  747.    return online;
  748. } /* tCD */
  749.  
  750. /*--------------------------------------------------------------------*/
  751. /*       t W a i t F o r N e t C o n n e c t                          */
  752. /*                                                                    */
  753. /*       Wait for remote system to connect to our waiting server      */
  754. /*--------------------------------------------------------------------*/
  755.  
  756. boolean tWaitForNetConnect(int timeout)
  757. {
  758.    fd_set readfds;
  759.    int nReady;
  760.    struct timeval tm;
  761.  
  762.    tm.tv_sec = timeout;
  763.    tm.tv_usec = 0;
  764.  
  765.    FD_ZERO(&readfds);
  766.    FD_SET(pollingSock, &readfds);
  767.  
  768.    nReady = select(1, &readfds, NULL, NULL, &tm);
  769.  
  770.    if (nReady == SOCKET_ERROR)
  771.    {
  772.       int wsErr = WSAGetLastError();
  773.  
  774.       printmsg(0, "WaitForNetConnect: select() failed");
  775.       printWSerror("select", wsErr);
  776.       return FALSE;
  777.    }
  778.    else if (nReady == 0)
  779.    {
  780.       printmsg(5, "WaitForNetConnect: select() timed out");
  781.       return FALSE;
  782.    }
  783.  
  784.    connectedSock = accept(pollingSock, NULL, NULL);
  785.    if (connectedSock == INVALID_SOCKET)
  786.    {
  787.       int wsErr = WSAGetLastError();
  788.  
  789.       printmsg(0, "WaitForNetConnect: could not accept a connection");
  790.       printWSerror("accept", wsErr);
  791.    }
  792.  
  793.    carrierDetect = TRUE;
  794.  
  795.    return TRUE;
  796.  
  797. } /* tWaitForNetConnect */
  798.  
  799. boolean IsFatalSocketError(int err)
  800. {
  801.    if (err == WSAENOTSOCK     ||
  802.        err == WSAENETDOWN     ||
  803.        err == WSAENETRESET    ||
  804.        err == WSAECONNABORTED ||
  805.        err == WSAECONNRESET   ||
  806.        err == WSAENOTCONN     ||
  807.        err == WSAECONNREFUSED ||
  808.        err == WSAEHOSTDOWN    ||
  809.        err == WSAEHOSTUNREACH)
  810.        return TRUE;
  811.     else
  812.        return FALSE;
  813. }
  814.